
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <link rel="stylesheet" href="./../../cesium/Cesium1.98/Widgets/widgets.css">
    <script type="text/javascript" src="./../../cesium/Cesium1.98/Cesium.js"></script>

</head>

<body style="margin: 0; overflow: hidden; background: #fff; width: 100%; height: 100%; position: absolute; top: 0">
    <div id="map" style="margin: 0 auto; width: 100%; height: 100%"></div>
    <script type="text/javascript">

        Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI3ZjQ5ZGUzNC1jNWYwLTQ1ZTMtYmNjYS05YTY4ZTVmN2I2MDkiLCJpZCI6MTE3MTM4LCJpYXQiOjE2NzY0NDUyODB9.ZaNSBIfc1sGLhQd_xqhiSsc0yr8oS0wt1hAo9gbke6M'
        const viewer = new Cesium.Viewer('map', {});

        // Cesium地形
        viewer.terrainProvider = Cesium.createWorldTerrain({
            requestWaterMask: true, // 请求水体效果所需要的海岸线数据
            requestVertexNormals: true// 请求地形照明数据
        });

        // 开启帧率
        viewer.scene.debugShowFramesPerSecond = true;

        // 多边形
        var polygonInstance = new Cesium.GeometryInstance({
            id: {
                "name": 'polygon'
            },
            geometry: new Cesium.PolygonGeometry({
                vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
                polygonHierarchy: new Cesium.PolygonHierarchy(
                    Cesium.Cartesian3.fromDegreesArray([
                        -120, 40,
                        -120, 30,
                        -110, 30,
                        -110, 40
                    ])
                ),
                // extrudedHeight:2000
            }),
            attributes: {
                color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.RED.withAlpha(0.5))
            }
        });
        viewer.scene.primitives.add(new Cesium.Primitive({
            geometryInstances: [polygonInstance],
            appearance: new Cesium.PerInstanceColorAppearance()
        }));

        // 线
        var lineInstance = new Cesium.GeometryInstance({
            id: {
                "name": 'line'
            },
            geometry: new Cesium.PolylineGeometry({
                positions: Cesium.Cartesian3.fromDegreesArray([
                    -100, 40,
                    -90, 40,
                    -120, 60,
                ]),
                width: 15,
                vertexFormat: Cesium.PolylineColorAppearance.VERTEX_FORMAT,
            }),
            attributes: {
                color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromCssColorString("#001aff").withAlpha(0.5))
            }
        })
        viewer.scene.primitives.add(new Cesium.Primitive({
            geometryInstances: [lineInstance],
            appearance: new Cesium.PolylineColorAppearance()
        }))

        // 发光的线
        viewer.scene.primitives.add(new Cesium.Primitive({
            geometryInstances: new Cesium.GeometryInstance({
                geometry: new Cesium.PolylineGeometry({
                    positions: Cesium.Cartesian3.fromDegreesArray([
                        -110, 40,
                        -100, 40,
                        -130, 60,
                    ]),
                    width: 20.0,
                    vertexFormat: Cesium.PolylineMaterialAppearance.VERTEX_FORMAT
                }),
                id: {
                    "name": '发光的线'
                },
            }),
            appearance: new Cesium.PolylineMaterialAppearance({
                material: Cesium.Material.fromType(Cesium.Material.PolylineGlowType, {
                    color: new Cesium.Color(1.0, 0.0, 0.0, 1.0)
                })
            })
        }));

        // -------Primitive点击事件-------------
        var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        handler.setInputAction(function (clickEvent) {
            var ray1 = viewer.camera.getPickRay(clickEvent.position);
            var cartesian = viewer.scene.globe.pick(ray1, viewer.scene);
            var pick = viewer.scene.pickPosition(clickEvent.position);
            var pickEd = viewer.scene.pick(clickEvent.position);
            if (pickEd && pick) {
                console.log(pickEd.id)
            }
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
        var flag = false;
        var models = viewer.scene.primitives._primitives;
        for (let i = 0; i < models.length; i++) {
            if (models[i]._url) {
                flag = true;
            }
        }
        console.log(flag)

    </script>
</body>

</html>